# Step 0 - Initial setup of this script
#          You need to make a custom field called windows_reimage that is Boolean and assigned to all
#          with a default value of False.
#          You need to make a Smart Group that includes devices with windows_reimage set to True
#          You need to associate this fileset with the Smart Group that is used for reimaging 
#          You need to pick properties for this script and change the first launch argument
#          so that your API token is used.

#############################################################################
#If Powershell is running the 32-bit version on a 64-bit machine, we
#need to force powershell to run in 64-bit mode.
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    #write-warning "Take me to 64-bit....."
    if ($myInvocation.Line) {
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
    }else{
        &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
    }
exit $lastexitcode
}
 
# Main script

# Step 1 - Set windows_reimage to false so that the device leaves the smart group 
#          that is used to associate with this fileset.

$token = $Env:token
$device = $Args[0]
$server = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\FileWave\WinClient).server
$timeout = 10
$flag=0
$date = Get-Date -Format "o"
$api = "https://" + $server +  ":20443/inv/api/v1/client/" + $device
$header = @{Authorization=$token}

try{
    $data = '{"CustomFields":{"windows_reimage":{"exitCode":null,"status":0,"updateTime":"' + $date + '","value":"' + $flag + '"}}}'
    Invoke-RestMethod -TimeoutSec $timeout -Method PATCH -Headers $header -Body $data -ContentType application/json -Uri $api
    }
catch{
    write-host "Something went wrong with the API"
    exit 9
    }

write-host "API Post Completed"

# Step 2 - Make sure recovery mode is enabled

$enaRea = 'c:\windows\system32\reagentc.exe'
start-Process -FilePath $enaRea -ArgumentList ('/enable') -Wait

# Step 3 - Initiate a wipe of the system. Check the Microsoft link below for alternative wipe options.

# This part wipes the system
# https://docs.microsoft.com/en-us/windows/client-management/mdm/remotewipe-csp
# methodname can be doWipeMethod or doWipeProtected but the later needs Win 10 1703 or newer
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_RemoteWipe"
$methodName = "doWipeMethod"

$session = New-CimSession

$params = New-Object Microsoft.Management.Infrastructure.CimMethodParametersCollection
$param = [Microsoft.Management.Infrastructure.CimMethodParameter]::Create("param", "", "String", "In")
$params.Add($param)

$instance = Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT' and InstanceID='RemoteWipe'"
$session.InvokeMethod($namespaceName, $instance, $methodName, $params)
 
#############################################################################
#End
#############################################################################